package cn.jksoft.businessclient.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import cn.jksoft.businessclient.R;
import cn.jksoft.businessclient.bean.ProductEntity;
import cn.jksoft.businessclient.bean.StoreServiceEntity;

/**
 * Author:  nick
 * Email:   nickdevp@gmail.com
 * Date:    16/4/11 15:15.
 * Description:
 */
public class ServiceExpandableListviewAdapter extends BaseExpandableListAdapter {

    private Context context;
    List<StoreServiceEntity.StoreServiceListEntity> groups;
    List<List<ProductEntity>> childs;
    ItemViewHolder holder;

    public class ItemViewHolder {
        public TextView serviceType;
        public TextView servicePrice;
    }

    public ServiceExpandableListviewAdapter(Context context,
                                            List<StoreServiceEntity.StoreServiceListEntity> groups,
                                            List<List<ProductEntity>> childs) {
        this.groups = groups;
        this.childs = childs;
        this.context = context;
    }


    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return childs.get(groupPosition).get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    // 获取二级列表的View对象
    @Override
    public View getChildView(final int groupPosition, final int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {
        final ItemViewHolder holder;
        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // 获取二级列表对应的布局文件, 并将其各元素设置相应的属性
            convertView = layoutInflater.inflate(R.layout.item_service_expandlistview_child, null);
            holder = new ItemViewHolder();
            this.holder = holder;
            convertView.setTag(holder);
            holder.serviceType = (TextView) convertView.findViewById(R.id.tv_service_type);
            holder.servicePrice = (TextView) convertView.findViewById(R.id.tv_service_price);
        } else {
            holder = (ItemViewHolder) convertView.getTag();
            resetViewHolder(holder);
        }

        holder.serviceType.setText(childs.get(groupPosition).get(childPosition).getPROD_NAME());
        holder.servicePrice.setText("￥"+ Double.parseDouble(childs.get(groupPosition).get(childPosition).getSALE_PRICE())/100);

        return convertView;
    }


    @Override
    public int getChildrenCount(int groupPosition) {
        return childs.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groups.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return groups.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    // 获取一级列表View对象
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // 获取一级列表布局文件,设置相应元素属性
            convertView = layoutInflater.inflate(R.layout.item_service_expandlistview_group, null);
        }
        TextView tvServiceName = (TextView) convertView.findViewById(R.id.tv_service_name);
        ImageView ivServiceImg = (ImageView) convertView.findViewById(R.id.iv_service_img);

        tvServiceName.setText(groups.get(groupPosition).getCATA_NAME());

        switch (groups.get(groupPosition).getPROD_CATA_ID()){
            case 21://洗车
                ivServiceImg.setImageResource(R.drawable.ic_service_wash);
                break;
            case 22://美容
                ivServiceImg.setImageResource(R.drawable.ic_service_beauty);
                break;
            case 23://保养
                ivServiceImg.setImageResource(R.drawable.ic_service_maintain);
                break;
            case 24://改装
                ivServiceImg.setImageResource(R.drawable.ic_service_refit);
                break;
            case 350://维修
                ivServiceImg.setImageResource(R.drawable.ic_service_repair);
                break;
            case 351://救援
                ivServiceImg.setImageResource(R.drawable.ic_service_rescue);
                break;
            default://其他
                ivServiceImg.setImageResource(R.drawable.ic_service_wash);
                break;
        }

        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    protected void resetViewHolder(ItemViewHolder viewHolder) {

    }


    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }


}
